import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.category.DefaultPieDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 数据填充及多元描述 {

    public static void main(String[] args) throws IOException {
        // 读取CSV文件
        CSVParser parser = CSVParser.parse(new FileReader("D:\\Marketing Campaign\\marketing_campaign.csv"),
                CSVFormat.DEFAULT.withDelimiter(';'));

        // 存储数据的列表
        List<Double> yearBirths = new ArrayList<>();
        List<Double> incomes = new ArrayList<>();
        List<Double> educations = new ArrayList<>();
        Map<String, Integer> educationCounts = new HashMap<>();
        Map<String, Integer> maritalStatusCounts = new HashMap<>();

        // 解析CSV文件
        for (CSVRecord record : parser) {
            yearBirths.add(Double.parseDouble(record.get(1)));
            incomes.add(Double.parseDouble(record.get(4)));
            double education = educationToNumber(record.get(2));
            educations.add(education);
            educationCounts.put(record.get(2), educationCounts.getOrDefault(record.get(2), 0) + 1);
            maritalStatusCounts.put(record.get(3), maritalStatusCounts.getOrDefault(record.get(3), 0) + 1);
        }

        // 线性回归模型
        OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
        double[][] xData = new double[yearBirths.size()][2];
        double[] yData = new double[incomes.size()];

        for (int i = 0; i < yearBirths.size(); i++) {
            xData[i][0] = yearBirths.get(i);
            xData[i][1] = educations.get(i);
            yData[i] = incomes.get(i);
        }

        regression.newSampleData(yData, xData);

        // 输出回归参数
        double[] beta = regression.estimateRegressionParameters();
        System.out.println("Intercept: " + beta[0]);
        System.out.println("Year Birth Coefficient: " + beta[1]);
        System.out.println("Education Coefficient: " + beta[2]);

        // 绘制散点图
        XYSeries series = new XYSeries("Data");
        for (int i = 0; i < yearBirths.size(); i++) {
            series.add(yearBirths.get(i), incomes.get(i));
        }
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        JFreeChart chart = ChartFactory.createXYLineChart(
                "出生年份与收入的关系",
                "出生年份",
                "收入",
                dataset,
                PlotOrientation.VERTICAL,
                true, true, false);
        XYPlot plot = chart.getXYPlot();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesShapesVisible(0, true);
        renderer.setSeriesLinesVisible(0, false);
        plot.setRenderer(renderer);
        ChartFrame frame = new ChartFrame("Scatter Plot", chart);
        frame.pack();
        frame.setVisible(true);

        // 绘制教育背景的饼状图
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        for (Map.Entry<String, Integer> entry : educationCounts.entrySet()) {
            pieDataset.setValue(entry.getKey(), entry.getValue());
        }
        JFreeChart pieChart = ChartFactory.createPieChart(
                "教育水平分布", pieDataset,
                true, true, false);
        ChartFrame pieFrame = new ChartFrame("Pie Chart", pieChart);
        pieFrame.pack();
        pieFrame.setVisible(true);

        // 绘制婚姻状况的柱状图
        DefaultCategoryDataset barDataset = new DefaultCategoryDataset();
        for (Map.Entry<String, Integer> entry : maritalStatusCounts.entrySet()) {
            barDataset.addValue(entry.getValue(), "Marital Status", entry.getKey());
        }
        JFreeChart barChart = ChartFactory.createBarChart(
                "婚姻状况分布",
                "婚姻状况",
                "数量",
                barDataset,
                PlotOrientation.VERTICAL,
                true, true, false);
        ChartFrame barFrame = new ChartFrame("Bar Chart", barChart);
        barFrame.pack();
        barFrame.setVisible(true);
    }

    private static double educationToNumber(String education) {
        switch (education) {
            case "Graduation":
                return 1;
            case "PhD":
                return 2;
            case "Master":
                return 3;
            case "2n Cycle":
                return 4;
            case "Basic":
                return 5;
            default:
                return 0;
        }
    }
}